home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’96 / Audio dcmds / Audio CD dcmds.sit / Audio CD dcmds.π / play dcmd / dcmd.c < prev    next >
Text File  |  1995-07-06  |  7KB  |  339 lines

  1. /*****************************************************
  2.  
  3.     bin.c                    P. Stadelmann, June 1994
  4.     
  5.     Displays a long word in binary.
  6.  
  7.  *****************************************************/
  8.  
  9.  
  10. #include <SetUpA4.h>    //dcmd.h
  11. #include <Think_dcmd.h>
  12. #include <Think_put.h>
  13. #include "cd.h"
  14. #include "toolutils.h"
  15. #define min(x,y) (x < y ? x : y )
  16. #define max(x,y) (x > y ? x : y )
  17.  
  18. void    PlayTrack(Byte startTrack);
  19. pascal Boolean    BitTest(const void *    data, long bitNum);
  20. void     DoAudioCommand(short command);
  21.  
  22. void DoHelp( void )
  23. {
  24.     PutPStr( "\pplay [Track # | P | E | S | ? | + | - ]");
  25.     PutLine();
  26.     PutPStr( "\p   Starts playing an audio CD. ");
  27.     PutLine();
  28.     PutPStr( "\p   Track # - Start playing from the provided track number");
  29.     PutLine();
  30.     PutPStr( "\p   P - Pause or resume play");
  31.     PutLine();
  32.     PutPStr( "\p   E - Eject the disk");
  33.     PutLine();
  34.     PutPStr( "\p   S - Stop playing");
  35.     PutLine();
  36.     PutPStr( "\p   ? - Show current track and time info");
  37.     PutLine();
  38.     PutPStr( "\p   + - Skip to the next track");
  39.     PutLine();
  40.     PutPStr( "\p   - - Skip to the previous track");
  41.     PutLine();
  42. }
  43.  
  44. void DoJob()
  45. {
  46.         long        value;
  47.         Boolean        ok;
  48.         Str255        myStr;
  49.         short        pos;
  50.     
  51.     switch(dcmdPeekAtNextChar()) {
  52.         case '\r':
  53.             PlayTrack(1);
  54.             break;
  55.         
  56.         case 'P':
  57.         case 'E':
  58.         case 'S':        
  59.         case 'p':
  60.         case 'e':
  61.         case 's':        
  62.         case '?':
  63.         case '+':
  64.         case '-':
  65.             pos = dcmdGetPosition();
  66.             dcmdGetNextExpression( &value, &ok );
  67.             DoAudioCommand(dcmdPeekAtNextChar());
  68.             break;
  69.             
  70.         case 'V':
  71.         case 'v':
  72.             dcmdGetNextParameter(myStr);
  73.             pos = dcmdGetPosition();
  74.             dcmdSetPosition(pos);
  75.             dcmdGetNextExpression( &value, &ok );
  76.             if(ok && value >= 0 && value <= 255) {
  77.                 OSErr    osErr;
  78.                 short ioRefNum;
  79.                 osErr = OpenCD(1, &ioRefNum);
  80.                 
  81.                 if(osErr != noErr) {
  82.                     PutLine();
  83.                     PutPStr( "\pCouldn't open CD Drive!");
  84.                     return;
  85.                 }
  86.                 if(!DiscInDrive(ioRefNum)) {
  87.                     PutLine();
  88.                     PutPStr( "\pNo disk in CD Drive!");
  89.                     return;
  90.                 }
  91.  
  92.                 PutPStr( "\pSetting volume to ");
  93.                 PutSDec(value);
  94.                 ASetVolume(ioRefNum, value);
  95.             }
  96.             else {
  97.                 PutPStr( "\pVolume not found or out of range");
  98.             }
  99.             break;
  100.             
  101.         default:
  102.             {
  103.                 pos = dcmdGetPosition();
  104.                 dcmdGetNextExpression( &value, &ok );
  105.         
  106.                 if (ok)
  107.                 {
  108.                     PutPStr( "\pstarting at track ");
  109.                     PutSDec(value);
  110.                     PutPStr("\p…");
  111.                     PlayTrack(value);        
  112.                 }
  113.                 else
  114.                     PutPStr( "\pSyntax error");
  115.             }
  116.             break;
  117.     }
  118.  
  119.     PutLine();
  120.  
  121. }
  122.  
  123. void SetVolume(short value)
  124. {
  125.  
  126. }
  127.  
  128. void     DoAudioCommand(short command)
  129. {
  130.     OSErr    osErr;
  131.     short    drive,
  132.             ioRefNum,
  133.             count,
  134.             track,
  135.             state;
  136.     Byte    userCDNumber,
  137.             userStart, 
  138.             userStop,
  139.             discStart,
  140.             discStop,
  141.             audioStatus,
  142.             playMode,
  143.             cntlField,
  144.             minutes,
  145.             seconds,
  146.             frames;
  147.     Byte        trackMin, trackSec, trackFrame;
  148.     Byte    currentTrack, discMin, discSec, discFrame;
  149.  
  150.  
  151.     userCDNumber    = 1;
  152.     userStart        = 0;
  153.     userStop        = 100;
  154.     playMode        = STEREO;
  155.  
  156.     osErr = OpenCD(userCDNumber, &ioRefNum);
  157.     if(osErr != noErr) {
  158.         PutLine();
  159.         PutPStr( "\pCouldn't open CD Drive!");
  160.         return;
  161.     }
  162.     if(!DiscInDrive(ioRefNum)) {
  163.         PutLine();
  164.         PutPStr( "\pNo disk in CD Drive!");
  165.         return;
  166.     }
  167.      
  168.     osErr = AStatus(ioRefNum, &audioStatus, &playMode, &cntlField, &minutes, &seconds, &frames);
  169.     
  170.     discStart = 0; 
  171.     do 
  172.         osErr = TrackInfo(ioRefNum, ++discStart, &cntlField, &minutes, &seconds, &frames);
  173.     while (BitTst(&cntlField, 5) && noErr == osErr); 
  174.     osErr = TrackCount(ioRefNum, &discStop);
  175.     osErr = ACurrTrackInfo(ioRefNum, ¤tTrack, &trackMin, &trackSec, &trackFrame);
  176.  
  177.  
  178.     switch (command) {
  179.         case 'p':
  180.         case 'P':
  181.             if(audioStatus == PAUSED) {
  182.                 PutPStr( "\pResuming");
  183.                 osErr = APause(ioRefNum, CONTINUE);
  184.             } else {
  185.                 if(audioStatus == PLAYING) {
  186.                     PutPStr( "\pPausing");
  187.                     osErr = APause(ioRefNum, PAUSE);
  188.                 }
  189.             }
  190.             break;
  191.             
  192.         case 'S':
  193.         case 's':
  194.             PutPStr( "\pStopping");
  195.             osErr = AStop(ioRefNum, 0);
  196.             break;
  197.     
  198.         case '+':
  199.             osErr = ATrkSearch( ioRefNum, ++currentTrack, audioStatus, playMode);
  200.             PutPStr( "\pswitching to track ");
  201.             PutSDec(currentTrack);
  202.             osErr = APause(ioRefNum, audioStatus);
  203.             break;
  204.         
  205.         case '-':
  206.             if((trackMin == 0) && (trackSec <=10)) {
  207.                 osErr = ATrkSearch( ioRefNum, --currentTrack, audioStatus, playMode);
  208.             }
  209.             else {
  210.                 osErr = ATrkSearch( ioRefNum, currentTrack, audioStatus, playMode);
  211.             }
  212.             PutPStr( "\pswitching to track ");
  213.             PutSDec(currentTrack);
  214.             osErr = APause(ioRefNum, audioStatus);
  215.             break;
  216.             
  217.         case '?':
  218.             PutPStr( "\pTrack ");
  219.             PutSDec(currentTrack);
  220.             PutPStr( "\p  ");
  221.             PutSDec(trackMin);
  222.             PutPStr( "\p:");
  223.             PutSDec(trackSec);
  224.             break;
  225.             
  226.         case 'E':
  227.         case 'e':
  228.             PutPStr( "\pEjecting disk");
  229.             osErr = EjectCD(ioRefNum);
  230.             break;    
  231.     }
  232.     
  233. }
  234.  
  235.  
  236. void    PlayTrack(Byte startTrack) 
  237. {
  238.     OSErr    osErr;
  239.     short    drive,
  240.             ioRefNum,
  241.             count,
  242.             track,
  243.             state;
  244.     Byte    userCDNumber,
  245.             userStart, 
  246.             userStop,
  247.             discStart,
  248.             discStop,
  249.             audioStatus,
  250.             playMode,
  251.             cntlField,
  252.             minutes,
  253.             seconds,
  254.             frames;
  255.     userCDNumber    = 1;
  256.     userStart        = 0;
  257.     userStop        = 100;
  258.     playMode        = STEREO;
  259.  
  260.     osErr = OpenCD(userCDNumber, &ioRefNum);
  261.     if(osErr != noErr) {
  262.         PutLine();
  263.         PutPStr( "\pCouldn't open CD Drive!");
  264.         return;
  265.     }
  266.     if(!DiscInDrive(ioRefNum)) {
  267.         PutLine();
  268.         PutPStr( "\pNo disk in CD Drive!");
  269.         return;
  270.     }
  271.  
  272.     osErr = AStatus(ioRefNum, &audioStatus, &playMode, &cntlField, &minutes, &seconds, &frames);
  273.     if(audioStatus == PAUSED) {
  274.         osErr = APause(ioRefNum, CONTINUE);
  275.     } else {
  276.     
  277.         discStart = 0; 
  278.         do 
  279.             osErr = TrackInfo(ioRefNum, ++discStart, &cntlField, &minutes, &seconds, &frames);
  280.         while (BitTst(&cntlField, 5) && noErr == osErr); 
  281.         osErr = TrackCount(ioRefNum, &discStop);
  282.  
  283.         if (noErr == osErr) {
  284.             if (startTrack == 0)  {
  285.                 osErr = AStop(ioRefNum, 0);
  286.             }
  287.             else {            
  288.                 osErr = AStop(ioRefNum, max(min(userStop, discStop), discStart));
  289.                 if(startTrack > 0) {
  290.                     userStart = startTrack;
  291.                     osErr = APlay(ioRefNum, startTrack, playMode);
  292.                 }
  293.                 else{
  294.                     osErr = APlay(ioRefNum, min(max(userStart, discStart), discStop), playMode);
  295.                 }
  296.             }
  297.         }
  298.     }
  299.     if(osErr) {
  300.         PutPStr( "\pGot error trying to play ");
  301.         PutSDec(osErr);
  302.     }
  303. }
  304.  
  305. //
  306. /*
  307. pascal Boolean    BitTest(const void *    data, long bitNum)
  308. {
  309.     unsigned long mask;
  310.     unsigned long    value;
  311.     
  312.     if(bitNum > 31) return false;
  313.     
  314.     bitNum = 31 - bitNum;
  315.     
  316.     value = *((unsigned long *)data);
  317.     mask = 1 << bitNum;
  318.     return ((value & mask) != 0);
  319. }
  320. */
  321.  
  322. pascal void CommandEntry( dcmdBlock* paramPtr )
  323. {
  324.     RememberA0();
  325.     SetUpA4();    
  326.     
  327.     switch ( paramPtr->request )
  328.     {
  329.         case dcmdInit : break;
  330.         
  331.         case dcmdHelp : DoHelp();
  332.                         break;
  333.                         
  334.         case dcmdDoIt : DoJob();
  335.                         break;
  336.     }
  337.  
  338.     RestoreA4();
  339. }